package com.warren.system.controller;


import com.warren.auth.entity.JwtUser;

import com.warren.response.Result;
import com.warren.system.dto.LoginRequest;
import com.warren.system.entity.Department;
import com.warren.system.entity.Menu;

import com.warren.system.entity.User;
import com.warren.system.service.DepartmentService;
import com.warren.system.service.MenuService;
import com.warren.system.service.UserService;
import com.warren.util.RedisUtils;
import com.wf.captcha.SpecCaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;

import java.security.Principal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;


/**
 * @author warren
 * @date 2021/3/28 0028 14:55
 * @Description
 **/
@RestController
@CrossOrigin
@Api(value = "权限认证",tags = "用户和管理员权限")
@RequestMapping
public class AuthController {

    private final UserService userService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private RedisUtils redisUtils;

    public AuthController(UserService userService){
        this.userService = userService;
    }


    @ResponseBody
    @ApiOperation(value = "验证码测试",notes = "验证码发送")
    @GetMapping(value = "/captcha2")
    public Result captcha2(){
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
        String verCode = specCaptcha.text();
        System.out.println(verCode);
        String key = UUID.randomUUID().toString();
        redisUtils.set(key,verCode,3600);
        return Result.ok().data("key",key).data("image",specCaptcha.toBase64());
    }

    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest loginRequest,HttpServletRequest request){
        System.out.println(loginRequest);

        String redisCode = (String) redisUtils.get(loginRequest.getVerKey());
        System.out.println(redisCode);
        return  userService.login(redisCode,loginRequest,request);
    }

    /**
     * 获取当前登录用户的信息
     *
     * @param principal
     * @return
     */
    @ApiOperation(value = "获取当前登录用户的信息")
    @GetMapping("/login/info")
    public Result getAdminInfo(Principal principal) {
        //Principal security的全局登陆用户
        System.out.println(principal);
        if (null == principal) {
            return null;
        }
        String username = principal.getName();
        User user = userService.getAdminByUserName(username);
        user.setPassword(null);
        JwtUser userInfo = new JwtUser(user);
        List<Menu> menus=menuService.getMenusByAdminId();
        Department department=departmentService.findDeptById(user.getDepartmentId());
        Set<String> urls = new HashSet<>();
        for (Menu menu : menus) {
            if (menu.getType().equals("1")&& !ObjectUtils.isEmpty(menu.getUrl())){
                urls.add(menu.getUrl());
            }
        }
        userInfo.setUrl(urls);
        userInfo.setName(department.getName());
        userInfo.setRoles(userService.getRole(user.getId()));
        return Result.ok().message("登陆用户信息").data("user", userInfo);
    }



    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public Result logout(){
        return Result.ok().message("注销成功");
    }
}
